using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using BCCommon.Enums;
using BCCommon.Extensions;
using BCData.Sim.Admin.Admin;
using BCData.Website.User.User;
using BCDto.Chats;
using BCDto.Chats.User;
using BCService.Utils;

namespace BCService.Chats
{
    public class UserResolverService : IUserResolverService
    {
        private static readonly ConcurrentDictionary<string, (UserDto User, DateTime Expires)> CachedUsers = new ConcurrentDictionary<string, (UserDto, DateTime)>();

        private readonly IIdGenerationService idGenerationService;
        private readonly IAdminData adminData;
        private readonly IUserData userData;

        public UserResolverService(IIdGenerationService idGenerationService, IAdminData adminData, IUserData userData)
        {
            this.idGenerationService = idGenerationService;
            this.adminData = adminData;
            this.userData = userData;
        }

        public UserDto Resolve(string chatUserId)
        {
            if (chatUserId.StartsWith($"{RoleType.Anonymous.ToString().ToUpperInvariant()}-"))
            {
                return new UserDto
                {
                    UserId = -1,
                    FullName = $"游客-{chatUserId.Substring(chatUserId.Length - 6)}"
                };
            }

            if (!CachedUsers.ContainsKey(chatUserId) || CachedUsers[chatUserId].Expires < DateTime.Now)
            {
                var user = this.InternalResolve(chatUserId);

                CachedUsers[chatUserId] = (user, DateTime.Now.AddMinutes(5));
            }

            return CachedUsers[chatUserId].User;
        }

        public AnonymousDto GenerateAnonymous()
        {
            var anonymousId = idGenerationService.GenerateId();

            return new AnonymousDto
            {
                AnonymousId = anonymousId,
                UserName = $"游客-{anonymousId.ToString().Substring(anonymousId.ToString().Length - 6)}"
            };
        }

        private UserDto InternalResolve(string chatUserId)
        {
            if (chatUserId.StartsWith($"{RoleType.Admin.ToString().ToUpperInvariant()}-"))
            {
                if (long.TryParse(chatUserId.Substring(6), out var realUserId))
                {
                    var admin = adminData.GetAdmin(realUserId).Result;

                    if (admin != null)
                    {
                        return new UserDto
                        {
                            UserId = realUserId,
                            FullName = admin.FullName,
                            RoleType = RoleType.Admin,
                            Phone = admin.Phone,
                            AvatarUrl = admin.Avatar
                        };
                    }
                }
            }

            if (chatUserId.StartsWith($"{RoleType.Partner.ToString().ToUpperInvariant()}-") || chatUserId.StartsWith($"{RoleType.Driver.ToString().ToUpperInvariant()}-") || chatUserId.StartsWith($"{RoleType.User.ToString().ToUpperInvariant()}-"))
            {
                if (long.TryParse(chatUserId.Substring(chatUserId.IndexOf("-") + 1), out var realUserId))
                {
                    var user = userData.GetUserByUserId(realUserId).Result;

                    if (user != null)
                    {
                        return new UserDto
                        {
                            UserId = realUserId,
                            FullName = user.UserName,
                            RoleType = user.Role.AsEnum<RoleType>(),
                            Phone = user.Phone,
                            AvatarUrl = user.Avatar
                        };
                    }
                }
            }

            return new UserDto { UserId = -1, FullName = "[已注销]" };
        }
    }
}